/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.IO;

namespace CrSyncFilesUtils
{

    public partial class CrSyncFileLocationSerializer
    {

        public CrSyncFileLocationSerializer()
        {
        }

        public CrSyncFileLocationSerializer(ICrSyncLocation location)
        {
            Path = location.Path;
            FileName = location.FileName;
            LocationType = location.LocationType;
            DriveType = location.DriveType;
            Uid = location.Guid;
            MetaDataDirectory = location.MetaDataDirectory;
        }

        public ICrSyncLocation GetFileLocation()
        {
            switch (LocationType)
            {
                case CrSyncFileLocationTypes.Local:
                    return new CrSyncFileLocationLocal(Path, FileName, MetaDataDirectory, Uid);
                case CrSyncFileLocationTypes.External:
                    return new CrSyncFileLocationExternal(Path, FileName, MetaDataDirectory, Uid);
                case CrSyncFileLocationTypes.Network:
                    return new CrSyncFileLocationNetwork(Path, FileName, MetaDataDirectory, Uid);
                default:
                    try
                    {
                        var methodInfos = GetType().GetMethods();
                        foreach (var methodInfo in methodInfos)
                        {
                            var hasGetFileLocationExtension = methodInfo.Name.Contains("GetFileLocationExtension");
                            if (hasGetFileLocationExtension)
                            {
                                var fileLocationResult = methodInfo.Invoke(this, null);
                                return (ICrSyncLocation) fileLocationResult;
                            }
                        }

                        return new CrSyncFileLocationLocal(Path, FileName, MetaDataDirectory, Uid);
                    }
                    catch (MissingMethodException)
                    {
                        return new CrSyncFileLocationLocal(Path, FileName, MetaDataDirectory, Uid);
                    }
            }
        }

        public Guid Uid { get; set; }
        public string Path { get; set; }
        public string FileName { get; set; }
        public string MetaDataDirectory { get; set; }
        public CrSyncFileLocationTypes LocationType { get; set; }
        public DriveType DriveType { get; set; }


    }
}
